package com.jtoon.servlet;

import java.io.IOException;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.session.PersistentManager;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.jtoon.bean.Comics;
import com.jtoon.dao.ComicsD;
import com.jtoon.util.PMF;

public class ComicsManage extends HttpServlet {
	
	private ComicsD comicsD;
	
	public ComicsManage(){
		comicsD = new ComicsD();
	}
	
	public static final int ADD = 1;
	public static final int UPDATE = 2;
	public static final int DELETE = 3;
	public static final int LIST = 4;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		int mode = 4;
		
		if(request.getParameter("mode") != null){
			mode = Integer.valueOf(request.getParameter("mode"));
		}
		  
		switch (mode) {
			case ADD :	comicsAdd(request, response); break;
			case UPDATE : comicsUpdate(request, response); break; 
			case DELETE : comicsDelete(request, response); break;
			case LIST : comicsList(request, response); break;
		}
		 
	} 
	
	private void comicsAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Comics.class);
		try{
			if(request.getParameter("title") != null){
				Comics comics = new Comics(
						request.getParameter("title"), 
						request.getParameter("author"), 
						request.getParameter("titleImg")
						);
				pm.makePersistent(comics);
			}
			List<Comics> list = (List<Comics>)query.execute(); 
			RequestDispatcher rd = request.getRequestDispatcher("/manage/comicsManage.jsp"); 
			request.setAttribute("comicsList", list);
			rd.forward(request, response);   
		}finally{
			pm.close(); 
			query.closeAll();
		}
	}
	 
	private void comicsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher rd = request.getRequestDispatcher("/manage/comicsManage.jsp"); 
		rd.forward(request, response);   
	}
	
	private void comicsDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String cid = request.getParameter("cid");
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		try{
			Comics comic = pm.getObjectById(Comics.class, Long.valueOf(cid));
			pm.deletePersistent(comic);
			
			Query query = pm.newQuery(Comics.class);
			List<Comics> list = (List<Comics>)query.execute();
			request.setAttribute("comicsList", list);	
			RequestDispatcher rd = request.getRequestDispatcher("/manage/comicsManage.jsp"); 
			rd.forward(request, response);     
		}finally{ 
			pm.close(); 
			//query.closeAll();  
		}
		
	} 
	
	private void comicsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Comics.class);
		try{
			List<Comics> list = (List<Comics>)query.execute(); 
			RequestDispatcher rd = request.getRequestDispatcher("/manage/comicsManage.jsp"); 
			request.setAttribute("comicsList", list);
			rd.forward(request, response);   
		}finally{
			pm.close(); 
			query.closeAll();
		}
	}
	
}
